﻿using Microsoft.AspNetCore.Cors.Infrastructure;

namespace WYJ.SDK.Web
{
    public class WYJCorsPolicy 
    {
        public List<string> ExposedHeaders { get; set; } = new List<string>();
        public List<string> Headers { get; set; } = new List<string>();
        public List<string> Methods { get; set; } = new List<string>();
        public List<string> Origins { get; set; } = new List<string>();

        /// <summary>
        /// 一个正 TimeSpan 值，指示预检请求可以缓存的时间
        /// </summary>
        public TimeSpan? PreflightMaxAge { get; set; } = TimeSpan.FromDays(1);
        public bool SupportsCredentials { get; set; }

        public static implicit operator CorsPolicy(WYJCorsPolicy corsPolicy)
        {
            var policy = new CorsPolicy()
            {
                PreflightMaxAge = corsPolicy.PreflightMaxAge,
                SupportsCredentials = corsPolicy.SupportsCredentials,
            };
            corsPolicy.ExposedHeaders.ForEach(x => policy.ExposedHeaders.Add(x));
            corsPolicy.Headers.ForEach(x => policy.Headers.Add(x));
            corsPolicy.Methods.ForEach(x => policy.Methods.Add(x));
            corsPolicy.Origins.ForEach(x => policy.Origins.Add(x));
            return policy;
        }
    }
}